package com.java4.exer;

import com.java3.util.JDBCUtils;

import java.util.List;
import java.util.Scanner;

/**
 * @author jingjintao
 * @date 2021/6/9 - 21:52
 **/
public class Exer2Test {


    public static void main(String[] args) {
//        testInsert();
//        queryWithIDCardOrExamCard();
        deleteByExamCard();
    }
    // 问题1：向examstudent表中添加一条记录
	/*
	 *  Type:
		IDCard:
		ExamCard:
		StudentName:
		Location:
		Grade:
	 */
    public static void testInsert(){
        final Scanner scanner = new Scanner(System.in);
        System.out.print("四级/六级：");
        int type = scanner.nextInt();
        System.out.print("身份证号：");
        String IDCard = scanner.next();
        System.out.print("准考证号：");
        String examCard = scanner.next();
        System.out.print("学生姓名：");
        String studentName = scanner.next();
        System.out.print("所在城市：");
        String location = scanner.next();
        System.out.print("考试成绩：");
        int grade = scanner.nextInt();
        String sql="insert into examstudent(type,IDCard,ExamCard,StudentName,Location,Grade) " +
                "values(?,?,?,?,?,?)";
        final int insertCount = JDBCUtils.update(sql, type, IDCard, examCard, studentName, location, grade);
        if(insertCount>0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    //问题2：根据身份证号或者准考证号查询学生成绩信息
    public static void queryWithIDCardOrExamCard(){
        System.out.println("请输入您要输入的类型");
        System.out.println("a.准考证号");
        System.out.println("b.身份证号");
        final Scanner scanner = new Scanner(System.in);
        final String choice = scanner.next();
        if("a".equals(choice)){
            System.out.print("请输入准考证号：");
            final String examCard = scanner.next();

            String sql="select FlowID flowID,Type type,IDCard IDCard,ExamCard,StudentName name," +
                    "Location location,Grade grade from examstudent where ExamCard =?";
            final List<Student> list = JDBCUtils.getForList(Student.class, sql, examCard);
            if(list.size()>0){
                System.out.println(list.get(0));
            }else{
                System.out.println("输入的准考证号有误!");
            }
        }else if("b".equals(choice)){
            System.out.print("请输入身份证号：");
            final String IDCard = scanner.next();

            String sql="select FlowID flowID,Type type,IDCard IDCard,ExamCard,StudentName name," +
                    "Location location ,Grade grade from examstudent where IDCard =?";
            final List<Student> list = JDBCUtils.getForList(Student.class, sql, IDCard);
            if(list.size()>0){
                System.out.println(list.get(0));
            }else{
                System.out.println("输入的身份证号有误!");
            }
        }else{
            System.out.println("您的输入有误，请重新进入程序");
        }
    }

    //问题3：删除指定的学生信息
    public static void deleteByExamCard(){
        System.out.println("请输入学生的考号：");
        final Scanner scanner = new Scanner(System.in);
        final String examCard = scanner.next();

        String sql="delete from examstudent where ExamCard = ?";
        final int delCount = JDBCUtils.update(sql, examCard);
        if(delCount>0){
            System.out.println("删除成功");
        }else {
            System.out.println("查无此人，请重新输入");
        }

    }
}
